//
//  JFBOrderGoodsProductDS.m
//  ios-supplier
//
//  Created by Benlai on 15/8/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBOrderGoodsProductDS.h"
/*                                                         原来的参数
 ///品牌ID
 @property (nonatomic, assign) NSUInteger brand_id;
 ///一级分类
 @property (nonatomic, assign) NSUInteger category1_id;
 ///库存
 @property (nonatomic, assign) NSUInteger inventory_qty;
 ///商品ID
 @property (nonatomic, assign) NSUInteger product_id;
 ///商家ID
 @property (nonatomic, assign) NSUInteger shop_id;
 ///商品状态   1为上架
 @property (nonatomic, assign) NSUInteger status;
 ///商品单位
 @property (nonatomic, strong) NSString *model_name;
 ///商品名称
 @property (nonatomic, strong) NSString *product_name;
 ///规格单位
 @property (nonatomic, strong) NSString *weight_name;
 ///规格
 @property (nonatomic, strong) NSString *weight_value;
 ///出货价
 @property (nonatomic, strong) NSDecimalNumber *out_price;
 */

/*
 @property (nonatomic, strong) NSArray *openTimes;
 //购买数量
 @property (nonatomic, assign) NSInteger buyCount;
 
 ///供应商ID
 @property (nonatomic,assign) NSInteger supplierId;
 ///供应商产品ID
 @property (nonatomic,assign) NSInteger productId;
 ///供应商产品库存数
 @property (nonatomic,assign) NSInteger productStock;
 ///供应商产品进货价，精确到小数点后两位
 @property (nonatomic,assign) NSInteger productPrice;
 ///供应商产品税率 无意义
 @property (nonatomic,assign) NSInteger taxRate;
 ///商品图片
 @property (nonatomic,  copy) NSString *photoUrl;
 ///商品名称
 @property (nonatomic,  copy) NSString *merchantProductName;
 ///商品规格
 @property (nonatomic,  copy) NSString *productSpec;
 ///商品ID
 @property (nonatomic,assign) NSInteger merchantProductId;
 ///商品编码
 @property (nonatomic,  copy) NSString *merchantProductCode;
 ///产品编码
 @property (nonatomic,  copy) NSString *productCode;
 ///产品名称
 @property (nonatomic,  copy) NSString *productName;
 ///产品条形码
 @property (nonatomic,  copy) NSString *productBarcode;
 ///产品类目ID
 @property (nonatomic,assign) NSInteger productCategoryId;
 ///产品类目名称
 @property (nonatomic,  copy) NSString *productCategoryName;
 ///产品品牌ID
 @property (nonatomic,assign) NSInteger productBrandId;
 ///产品品牌名称
 @property (nonatomic,  copy) NSString *productBrandName;
 ///是否精品 ,true 精品   false非精品
 @property (nonatomic,assign) NSInteger isHighQuality;
 */

@implementation JFBOrderGoodsProductDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.supplierId = [[attributes objectForKey: @"supplierId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"supplierId"]  integerValue];
    self.productId = [[attributes objectForKey: @"productId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"productId"]  integerValue];
    self.productStock = [[attributes objectForKey: @"productStock"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"productStock"]  integerValue];
    self.productPrice = [[attributes objectForKey: @"productPrice"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"productPrice"];
    self.taxRate = [[attributes objectForKey: @"taxRate"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"taxRate"]  integerValue];

    self.photoUrl = [[attributes objectForKey: @"photoUrl"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"photoUrl"];
    self.merchantProductName = [[attributes objectForKey: @"merchantProductName"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"merchantProductName"];
    self.productSpec = [[attributes objectForKey: @"productSpec"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"productSpec"];
    self.merchantProductId = [[attributes objectForKey:@"merchantProductId"] isEqual:[NSNull null]] ? 0:[[attributes objectForKey:@"merchantProductId"] integerValue];
    self.merchantProductCode = [[attributes objectForKey:@"merchantProductCode"] isEqual:[NSNull null]] ? 0:[attributes objectForKey:@"merchantProductCode"];
    self.productCode = [[attributes objectForKey:@"productCode"]isEqual:[NSNull null]]?0:[attributes objectForKey:@"productCode"];
    self.productName = [[attributes objectForKey:@"productName"]isEqual:[NSNull null]]?0:[attributes objectForKey:@"productName"];
    self.productBarcode = [[attributes objectForKey:@"productBarcode"]isEqual:[NSNull null]]?0:[attributes objectForKey:@"productBarcode"];
    self.productCategoryId = [[attributes objectForKey:@"productCategoryId"]isEqual:[NSNull null]]?0:[[attributes objectForKey:@"productCategoryId"] integerValue];
    self.productCategoryName = [[attributes objectForKey:@"productCategoryName"]isEqual:[NSNull null]]?0:[attributes objectForKey:@"productCategoryName"];
    self.productBrandId = [[attributes objectForKey:@"productBrandId"]isEqual:[NSNull null]]?0:[[attributes objectForKey:@"productBrandId"] integerValue];
    self.productBrandName = [[attributes objectForKey:@"productBrandName"]isEqual:[NSNull null]]?0:[attributes objectForKey:@"productBrandName"];
    self.isHighQuality = [[attributes objectForKey:@"isHighQuality"]isEqual:[NSNull null]]?0:[[attributes objectForKey:@"isHighQuality"] integerValue];
    return self;
}

+ (NSURLSessionDataTask *)loadProductList:(void (^)(NSArray *menuList, NSError *error))block withUT:(NSString *)ut withLabelId:(NSInteger)labelId withCompanyId:(NSInteger)companyId withCateTreeId:(NSInteger)cateTreeId withCateTreeNodeId:(NSInteger)cateTreeNodeId withLimitClauseCount:(NSInteger)limitClauseCount withLimitClauseStart:(NSInteger)limitClauseStart{
    NSDictionary * parameters = @{@"ut":ut,
                                  @"merchantId":[UserInfo currentUser].merchantID,
                                  @"companyId":@(companyId),
                                  @"cateTreeId":@(cateTreeId),
                                  @"cateTreeNodeId":@(cateTreeNodeId),
                                  @"limitClauseCount":@(limitClauseCount),
                                  @"limitClauseStart":@(limitClauseStart)
                                  };
    
    return [[JFBAPIClient sharedClient:false] GET: @"/opms-api/getSupplierMerchantProductList.do" parameters: parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"code"];
        NSLog(@"%@", JSON);
        if( [error isEqual:@"0"]) {
            NSMutableArray *result = [@[] mutableCopy];
            NSDictionary *subDic = [JSON valueForKeyPath:@"data"];
            if (![[subDic valueForKey:@"rows"]isEqual:[NSNull null]]) {
                NSArray *dataArray = [subDic valueForKey:@"rows"];
                for( NSDictionary *product in dataArray) {
                    JFBOrderGoodsProductDS * ds = [[JFBOrderGoodsProductDS alloc] initWithAttributes:product];
                    [result addObject:ds];
                }
                
            }
           block([NSArray arrayWithArray:result], nil);
        }else if ([JSON[@"code"] integerValue] == 99){
            UIViewController* viewController;
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
        }else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}
@end
